{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import sys\n",
    "sys.path.append(os.path.abspath('../'))\n",
    "from common import *\n",
    "import configs\n",
    "import opt_configs\n",
    "\n",
    "from constants import OUTPUT_DIR\n",
    "import matplotlib.gridspec as gridspec\n",
    "import matplotlib.patches as patches\n",
    "\n",
    "import json\n",
    "\n",
    "from conv_opt_results import results\n",
    "\n",
    "fig_name = 'conv_opt_results'\n",
    "fig_dir = join(FIGURE_DIR, fig_name)\n",
    "used_configs = [configs.ConvolutionWarp4(), configs.ConvolutionWarp8(), configs.ConvolutionWarp16(), configs.Warp()]\n",
    "names = ['Convolution, 4 samples', 'Convolution, 8 samples', 'Convolution, 16 samples', '\\\\textbf{Ours}']\n",
    "\n",
    "fontsize = 16\n",
    "base_size = 3\n",
    "n_rows = len(results)\n",
    "n_cols = 2 * len(used_configs) + 1\n",
    "total_width = base_size * (len(used_configs) + 1 + 0.5 * len(used_configs))\n",
    "fig = plt.figure(1, figsize=(total_width, 0.98 * n_rows * base_size), constrained_layout=False)\n",
    "width_ratios = [1, ] * n_cols\n",
    "for col in range(1, n_cols):\n",
    "    if col % 2 == 0:\n",
    "        width_ratios[col] /= 2\n",
    "\n",
    "gs = fig.add_gridspec(n_rows, n_cols, wspace=0.025, hspace=0.025, width_ratios=width_ratios)\n",
    "\n",
    "inset_size = 0.2\n",
    "y_offset = -0.08\n",
    "\n",
    "ranges = [5e-7, 2e-6, 5e-5]\n",
    "for row, result in enumerate(results):\n",
    "    scene, opt_name = result['scene'], result['opt_config']\n",
    "\n",
    "    opt_config = opt_configs.get_opt_config(opt_name)\n",
    "    output_dir = join(OUTPUT_DIR, scene, opt_config.name, used_configs[0].name)\n",
    "    ax = fig.add_subplot(gs[row, 0])\n",
    "    img = read_img(join(output_dir, f'ref-{result[\"ref_view\"]:02d}.exr'))\n",
    "    ax.imshow(img, interpolation='none')\n",
    "    disable_ticks(ax)\n",
    "    ax.set_ylabel('\\\\textsc{' + result[\"pretty_name\"] + '}', labelpad=5)\n",
    "    if row == n_rows - 1:\n",
    "        txt = ax.set_title('Scene', fontsize=fontsize, y=y_offset, va='top')\n",
    "\n",
    "    col = 1\n",
    "    r = None\n",
    "    for cfg in used_configs:\n",
    "        output_dir = join(OUTPUT_DIR, scene, opt_config.name, cfg.name)\n",
    "        ax = fig.add_subplot(gs[row, 2 * col - 1])\n",
    "        fn = join(FIGURE_DIR, fig_name, scene, f'{cfg.name}_final.exr')\n",
    "        img = read_img(fn, background_color=[1, 1, 1])\n",
    "        ax.imshow(img, interpolation='none', extent=[0, 1, 0, 1])\n",
    "        disable_ticks(ax)\n",
    "\n",
    "        with open(join(output_dir, f'metadata.json'), 'r') as f:\n",
    "            stats = json.load(f)\n",
    "\n",
    "        txt = ax.text(0.99, 0.0, time_to_string(stats[\"total_time\"]), size=14, ha=\"right\", va=\"bottom\")\n",
    "        txt.set_path_effects([path_effects.withStroke(linewidth=1.0, foreground='white')])\n",
    "\n",
    "        if row == n_rows - 1 and col > 0:\n",
    "            txt = ax.set_title(names[col - 1], fontsize=fontsize, y=y_offset, va='top')\n",
    "\n",
    "        gs_insets = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs[row, 2 * col], wspace=0.00, hspace=0.05)\n",
    "        for i, inset in enumerate(result['insets']):\n",
    "            if col == 1:\n",
    "                rect = patches.Rectangle((inset[1], 1 - inset[0] - inset_size), inset_size,\n",
    "                                         inset_size, linewidth=0.5, edgecolor=[0, 0.2, 0.4], facecolor='none')\n",
    "                ax.add_patch(rect)\n",
    "\n",
    "            ax_inset = fig.add_subplot(gs_insets[i])\n",
    "            h, w = img.shape[:2]\n",
    "            inset_size_px = int(max(h, w) * inset_size)\n",
    "            c0 = int(h * inset[0])\n",
    "            c1 = int(w * inset[1])\n",
    "            img_crop = img[c0:(c0 + inset_size_px), c1:(c1 + inset_size_px), :]\n",
    "            ax_inset.imshow(img_crop, interpolation='none')\n",
    "            disable_ticks(ax_inset)\n",
    "        col += 1\n",
    "\n",
    "plt.margins(0, 0)\n",
    "# save_fig(fig_name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('mi')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "c8cc192903caa17681aa39d71202092ac11a526d37a1c4ad2948f13605924304"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
